package utils

import (
	"io"
	"os"

	"github.com/google/uuid"
)

func GetHomeDir() string {
	return os.Getenv("HOME")
}

func GetEnvDefault(key string, default_value string) string {
	if res := os.Getenv(key); res != "" {
		return res
	} else {
		return default_value
	}
}

func GetUUID() string {
	uid, _ := uuid.NewUUID()
	return uid.String()
}

//
func WriteFile(filename string, data []byte, perm os.FileMode) error {
	f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
	if err != nil {
		return err
	}
	n, err := f.Write(data)
	if err == nil && n < len(data) {
		err = io.ErrShortWrite
	}
	if err1 := f.Close(); err == nil {
		err = err1
	}
	return err
}
